package com.leyou.page.service;

import com.leyou.item.client.ItemClient;
import com.ly.plus.dto.SpecGroupDTO;
import com.ly.plus.dto.SpuDTO;
import com.ly.plus.entity.Brand;
import com.ly.plus.entity.Category;
import com.ly.plus.entity.Sku;
import com.ly.plus.entity.SpuDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ycs
 * Date: 2020/8/27
 * Time: 14:57
 *
 * @author: ycs
 * Description: No Description
 */
@Service
public class PageService {
    @Autowired
    private ItemClient itemClient;
    @Value("${ly.static.itemDir}")
    private String itemDir;
    @Value("${ly.static.itemTemplate}")
    private String itemTemplate;
    @Autowired
    private SpringTemplateEngine templateEngine;
    /**
     * 提供商品详情页信息
     * @param spuId
     * @return
     */
    public Map<String, Object> toGoodsDetailPage(Long spuId) {
      //页面需要:categories,brand,spuName.subTitle,detail,skus,specs                                                                                                                                                                                                                               ,skus,specs
        //封装放回值
        HashMap<String, Object> resultMap = new HashMap<>(7);
        //1.根据spuId获取spu对象
        SpuDTO spuDTO = itemClient.findSpuById(spuId);
        //2,根据spuId获取Spudetail
        SpuDetail spuDetail = itemClient.findSpuDetailById(spuId);
        //根据品牌id获取品牌
        Brand brand = itemClient.findBrandById(spuDTO.getBrandId());
        //根据分类id集合获取分类集合
        List<Category> categorys = itemClient.findCategorysByIds(spuDTO.getCategoryIds());
        //根据spuId获取skus
        List<Sku> skus = itemClient.findSkuBySpuId(spuId);
        //根据分类id获取规格组
        List<SpecGroupDTO> specGroupDTOsByCid = itemClient.findSpecGroupDTOsByCid(spuDTO.getCid3());
        resultMap.put("categories",categorys);
        resultMap.put("brand",brand);
        resultMap.put("spuName",spuDTO.getName());
        resultMap.put("subTitle",spuDTO.getSubTitle());
        resultMap.put("detail",spuDetail);
        resultMap.put("skus",skus);
        resultMap.put("specs",specGroupDTOsByCid);
        return resultMap;
    }
    /**
     * 创建静态画页面
     */
    public void CreateStaticPage(Long spuId)  {
        //创建上下文对象
        Context context=new Context();
        //获取静态面数据
        context.setVariables(toGoodsDetailPage(spuId));
        //静态页面名
        File file = new File(itemDir);
        String pageName =spuId+".html";
        try(Writer writer=new PrintWriter(new File(file,pageName))) {
            templateEngine.process(itemTemplate,context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除静态页面
     * @param spuId
     */
    public void delItem(Long spuId) {
        String PageName=spuId+".html";
        File itemPagePath = new File(new File(itemDir), PageName);
        if (itemPagePath.exists()){
            itemPagePath.delete();
        }

    }
}
